<?php
/**
 * 监理规划控制器
 * @author YangL
 **/
namespace Admin\Controller;
use Common\Controller\WahahabaseController;
class SupervisionPlanController extends WahahabaseController{
	protected $users_model;
	public function _initialize() {
		parent::_initialize();
		$this->users_model = D("Common/Users");
		$this->deal_model = D("Common/SupervisionPlan");
		$this->att_model = D("Common/Attachment");
	}
	
	/**
     *  主页
     */
	public function index(){
		$where = array("d.is_deleted"=>array('neq', 1));
        $this->getDepartLevel();		
		$this->indexPage($where);
	}

	/**
     *  添加
     */
	public function add(){
		// 查询当前登录用户真是姓名
		$usname = $this->users_model->where(array("id" => session('ADMIN_ID')))->getField("real_name", true);
		$this->assign("us_name",$usname[0]);
		$this->assign("add_time", date('Y-m-d H:i:s'));
		
		$this->display();
	}
    public function add_post(){
		if(IS_POST){
			if(!I("post.fileName")){
				$this->error("请选择需要上传的文件！");
			}
			
			// 合并底层数据
			$data = $this->saveData();
			if ($this->deal_model->create()!==false) {
				$result = $this->deal_model->add($data);
				if ($result!==false) {
					$this->saveFile($result, 1);
					$this->success("添加成功！", U("SupervisionPlan/index"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->deal_model->getError());
			}
		}
	}
	  
   	public function view(){
     	$id = I('get.id',0,'intval');
		$result = $this->deal_model->alias("d")
			->field('d.*,u.real_name')
			->join(array(
				' LEFT JOIN '.C('DB_PREFIX').'users u ON d.add_user = u.id',
			))->where(array("d.id"=>$id))->find();
		$results = $this->att_model->where(array("table_id"=>$id, "belong_to"=>1))->select();
		
		$this->assign("result", $result);
		$this->assign("results", $results);
		$this->getDepartLevel();
		$this->display();
	}

	/**
     *  修改
     */
	public function edit(){
		$id = I('get.id',0,'intval');
		$result = $this->deal_model->alias("d")
			->field('d.*,u.real_name')
			->join(array(
				' LEFT JOIN '.C('DB_PREFIX').'users u ON d.add_user = u.id',
			))->where(array("d.id"=>$id))->find();
		$results = $this->att_model->where(array("table_id"=>$id, "belong_to"=>1))->select();
		
		$this->assign("result", $result);
		$this->assign("results", $results);
		
		$this->display();
	}
    public function edit_post(){
		if (IS_POST) {
			$data = array_merge($_POST, $this->saveData(2));
			if ($this->deal_model->create()!==false) {
				$result=$this->deal_model->save($data);
				if ($result !== false) {
					$this->saveFile(I("post.id"), 1, 2);
					$this->success("保存成功！", U("SupervisionPlan/index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->sPlan_model->getError());
				}
			}
	}
}
?>